package com.sky.controller.admin;

import com.sky.dto.FindOrdersDTO;
import com.sky.dto.IdDto;
import com.sky.entity.Order;
import com.sky.exception.BusinessException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.IOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import com.sky.vo.OrdersFindVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    /**
     * 各个状态的订单数量统计
     * @return :返回实体
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statistics(){
        OrderStatisticsVO orderStatisticsVO=orderService.getsSatistics();
        System.out.println("其实你最棒的");
        return Result.success(orderStatisticsVO);
    }

    /**
     * 查询订单数据
     * @param page
     * @param pageSize
     * @param status
     * @param beginTime
     * @param endTime
     * @param number
     * @param phone
     * @return
     */
    @GetMapping("/conditionSearch")
    public Result<PageResult<OrdersFindVO>> conditionSearch(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                                            @RequestParam(value = "status",required = false)Integer status,
                                                            @RequestParam(value = "beginTime",required = false)String beginTime,
                                                            @RequestParam(value = "endTime",required = false)String endTime,
                                                            @RequestParam(value = "number",required = false)String number,
                                                            @RequestParam(value = "phone",required = false)String phone){
        // log.info("{},{},{}",page,pageSize,status);测试成功
        //封装成FindOrdersDTO
        FindOrdersDTO findOrdersDTO = FindOrdersDTO.builder()
                .beginTime(beginTime)
                .phone(phone)
                .endTime(endTime)
                .number(number)
                .page(page)
                .pageSize(pageSize)
                .status(status).build();
        PageResult<OrdersFindVO> orderPageResult=orderService.conditionSearch(findOrdersDTO);
            return Result.success(orderPageResult);
    }

    /**
     * 根据订单主键查询订单详细数据
     * @param id ：订单主键id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result<OrderVO> details(@PathVariable(value = "id")Long id){
        OrderVO orderVO= orderService.details(id);
        if (orderVO==null){
            //防止测试出错，找开发的麻烦 这里抛出一个业务异常
            throw new BusinessException("输入的"+id+"是不合法数据");
        }else {
            return Result.success(orderVO);
        }
    }

    /**
     * 接单 asgbd
     * @param
     * @return
     */
    @PutMapping("/confirm")
    public Result<String> confirm(@RequestBody IdDto idDto){
        orderService.confirm(idDto.getId());
        return Result.success();
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    public Result<String> delivery(@PathVariable(value = "id")Long id){
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 根据id修改员工状态信息
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    public Result<String> complete(@PathVariable(value = "id")Long id){
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 客户催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    public Result<String> reminder(@PathVariable(value = "id")Long id){
        orderService.reminder(id);
        return Result.success("催单成功!");
    }

}
